
package com.aris.voicechecking;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.voicecheck.adapter.DayLessonAdapter;
import com.voicecheck.adapter.DayLessonAdapter.OnItemClickListener;
import com.voicecheck.model.DayLesson;
import com.voicecheck.provider.DataProviderV2;
import com.voicecheck.support.DividerItemDecoration;

public class DayLessonActivity extends ActionBarActivity {

    protected RecyclerView listLesson;

    protected DayLessonAdapter lessonAdapter;

    protected List<DayLesson> listLessonTitle;

    protected int weeknumber;

    private RelativeLayout adView;

    private DataProviderV2 dataConnection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_day_lesson);

        initital();
        execution();

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        setData();
    }

    private void initital() {
        listLesson = (RecyclerView)findViewById(R.id.list_day_lesson);
        listLessonTitle = new ArrayList<DayLesson>();
        lessonAdapter = new DayLessonAdapter(listLessonTitle);
        weeknumber = getIntent().getExtras().getInt("weeknumber");
        adView = (RelativeLayout)findViewById(R.id.adView);
        dataConnection = new DataProviderV2(this);
    }

    private void execution() {
        adView.setBackgroundResource(R.drawable.banner);

        adView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent webviewIntent = new Intent(DayLessonActivity.this, WebviewActivity.class);
                startActivity(webviewIntent);
                overridePendingTransition(R.drawable.anim_slide_in_left,
                        R.drawable.anim_slide_out_left);

                // Uri uriUrl = Uri.parse(ConnectionProvider.BANNER_URL);
                // Intent launchBrowser = new Intent(Intent.ACTION_VIEW,
                // uriUrl);
                // startActivity(launchBrowser);
                //
                // overridePendingTransition(R.drawable.anim_slide_in_left,
                // R.drawable.anim_slide_out_left);
            }
        });

        // setData();
        listLesson.addItemDecoration(new DividerItemDecoration(this, null));

        // configuration actionbar

        // getSupportActionBar().setTitle(getString(R.string.back));
        ActionBar actionBar = getSupportActionBar();
        ColorDrawable colorDrawable = new ColorDrawable(getResources().getColor(R.color.main_color));
        actionBar.setBackgroundDrawable(colorDrawable);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP);

        View actionbarcustomeview = getLayoutInflater().inflate(R.layout.actionbar_center_title,
                (ViewGroup)(getSupportActionBar().getCustomView()));

        TextView center_title = (TextView)actionbarcustomeview.findViewById(R.id.center_title);
        center_title.setText(getString(R.string.week) + " " + weeknumber);
        center_title.setTextColor(Color.WHITE);

        actionBar.setCustomView(actionbarcustomeview, new ActionBar.LayoutParams(
                ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.MATCH_PARENT,
                Gravity.CENTER));
    }

    private void setData() {

        listLesson.setAdapter(null);

        listLessonTitle.clear();

        int numOfday = dataConnection.getDayNumber(weeknumber);

        for (int i = 1; i <= numOfday; i++) {
            DayLesson dl = dataConnection.retrieveInfomationOfDayLesson(weeknumber, i);

            if (dl != null) {
                dl.setStt(i);
                listLessonTitle.add(dl);
            }
        }

        listLesson.setHasFixedSize(true);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        listLesson.setLayoutManager(mLayoutManager);

        listLesson.setAdapter(lessonAdapter);

        lessonAdapter.SetOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(View view, int position) {

                Log.d("day selected", "position" + position);
                Intent dayintent = new Intent(DayLessonActivity.this, LessonDetailActivity.class);

                dayintent.putExtra("daynumber", position + 1);
                dayintent.putExtra("weeknumber", weeknumber);

                startActivity(dayintent);

                overridePendingTransition(R.drawable.anim_slide_in_left,
                        R.drawable.anim_slide_out_left);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.day_lesson, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        overridePendingTransition(R.drawable.anim_slide_in_right, R.drawable.anim_slide_out_right);
    }

}
